<template>
  <section>
    <!-- 面包屑 -->
 <breadcrumbs></breadcrumbs>
    <div class="top-box">
      <div class="top-item"><img src="../../assets/images/leijiyizhichu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData/100}}</span><br>
            <span class="fontText">累计已支出(元)</span>
          </p>
        </div>
      </div>
    </div>
    <h1 class="title-style">会员累计回收支出明细</h1>
    <!-- 查询条件 -->
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      <el-form-item label="订单号码"
                    prop="name">
        <el-input v-model="searchForm.info"
                  placeholder="请输入订单号"></el-input>
      </el-form-item>
      <el-form-item label="输入搜索"
                    prop="keywords">
        <el-input v-model="searchForm.keywords"
                  placeholder="请输入会员昵称/手机号"></el-input>
      </el-form-item>
      <el-form-item label="搜索时间段"
                    prop="date">
        <el-date-picker v-model="searchForm.date"
                        type="daterange"
                        align="right"
                        unlink-panels
                        range-separator="至"
                        start-placeholder="开始日期"
                        value-format="yyyy-MM-dd"
                        format="yyyy-MM-dd"
                        end-placeholder="结束日期"
                        :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <btns @exec="exec"
          class="marbt20px"></btns>
    <div>
      <!-- <p>当前会员回收总计：&nbsp;&nbsp;<span>{{totalmoney}}</span> -->
        <!-- <el-button class="exports-btn"
                   @click="goExports"
                   type="primary">导出</el-button> -->
      </p>
    </div>
    <div class="data-container scroll-table">
      <!-- 按钮列表 -->
      <el-table :data="tableData"
                border
                style="width: 100%"
                @selection-change="tableSelectionChange">
        <el-table-column type="selection"
                         width="40">
        </el-table-column>
        <el-table-column type="index"
                         label="序号"
                         width="50">
        </el-table-column>
        <el-table-column prop="backOrderNo"
                         label="订单号">
        </el-table-column>
        <el-table-column prop="nickname"
                         label="用户昵称">
        </el-table-column>
        <el-table-column prop="phoneNum"
                         label="电话号码">
        </el-table-column>
        <el-table-column prop="brandName"
                         label="品牌"
                        >
        </el-table-column>
        <el-table-column prop="backCount"
                         label="数量">
                <template slot-scope="scope">
                <div v-if="scope.row.state==0">{{scope.row.backCount}}</div>
                <div v-if="scope.row.state==1">{{scope.row.realBackCount}}</div>
                <div v-if="scope.row.state==2">{{scope.row.backCount}}</div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="price"
                         label="单价">
        </el-table-column> -->
        <el-table-column prop="total"
                         label="总价(元)">
            <template slot-scope="scope">
                <div v-if="scope.row.state==0">{{scope.row.backMoney}}</div>
                <div v-if="scope.row.state==1">{{scope.row.realBackMoney}}</div>
                <div v-if="scope.row.state==2">{{scope.row.backMoney}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="addtime"
                         label="提交时间">
        </el-table-column>
        <el-table-column prop="checkTime"
                         label="审核时间">
        </el-table-column>
        <el-table-column prop="stopName"
                         label="审核回收站点">
        </el-table-column>
        <el-table-column prop="checkName"
                         label="审核人">
        </el-table-column>
        <el-table-column prop="state"
                         label="状态">
          <template slot-scope="scope">
            
            <div v-if="scope.row.state==0">审核中</div>
            <div v-if="scope.row.state==1">审核通过</div>
            <div v-if="scope.row.state==2">审核失败</div>
      
          </template>
        </el-table-column>
        <el-table-column prop="state"
                         label="操作">
          <template slot-scope="scope">
            <el-button @click="showDetails(scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="mgt-10 pd-0"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="searchForm.current"
                     :page-sizes="[10, 20, 30, 50]"
                     :page-size="searchForm.size"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="searchForm.totalCount">
      </el-pagination>
    </div>
    <!-- 新增消息 -->
    <el-dialog
        title="确认结算"
        :visible.sync="dialogVisible1"
        width="800px"
        center>
        <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px" class="demo-addForm">
            <el-form-item label="结算账期" prop="resource">
               
            </el-form-item>
            <el-form-item label="选择结算回收站">
                <el-row :gutter="20">
                    <el-col :span="10">
                        <el-cascader :props="optionProps" v-model="regionId" :options="city" @change="handleChange" clearable></el-cascader>
                    </el-col>
                    <el-col :span="10">
                        <el-input placeholder="请输入主体关键字搜索" v-model="subject" class="input-with-select">
                            <el-button slot="append" icon="el-icon-search" @click="getHouseBackStopList()"></el-button>
                        </el-input>
                    </el-col>
                </el-row>
            </el-form-item>
            <el-form-item>
                <el-transfer :titles="['请选择', '已选择']"
                       filterable
                       filter-placeholder="请输入按钮名称"
                       :props="{key: 'backStopId',label: 'stopName'}"
                       v-model="backStopId"
                       :data="backstopList"></el-transfer>
            </el-form-item>
            <el-form-item label="总结算金额" prop="title">
                <el-input v-model="addForm.title"></el-input>
            </el-form-item>

            <el-form-item class="mgt-100">
                <el-button type="primary" @click="submitForm('addForm')">确认结算</el-button>
                <el-button @click="dialogVisible1=false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

  </section>
</template>

<script>

import btns from '@/components/btns.vue'
import { format } from "../../utils/tool"
import ExportExcel from "../../utils/ExportExcel.js";
import breadcrumbs from "@/components/breadcrumbs.vue";
export default {
  components: {
    btns,
    ExportExcel,
     breadcrumbs,
  },
  data () {
    return {
      huishoulist:[],
      dialogVisible1:false,
      addForm: {},
      yearData: "",
      topData: {},
      btnForm: {},
      searchForm: {
        current: 1,
        size: 10,
        info: "",
        sid: "",
      },
      totalmoney: '',
      addORUpdateVisible: false,
      funTit: '',
      cityForm: {
        cityName: "",
        cityId: ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      rules: {
        "btnIcon": [],
        "btnName": [
          { required: true, message: '请输入城市名称', trigger: 'blur' }
        ],
        "btnCode": [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ],
        "sort": [
          { required: true, message: '排序', trigger: 'blur' }
        ]
      },
      tableData: [],
      tableSelection: []
    }
  },
  created () {
    // this.btnlist();
    // this.getYearData();
    this.getTableData(1, 10, this.searchForm.info, this.searchForm.sid);
  },
  methods: {
    goExports () {
      this.$api.finance.getUserOrderCostExcel().then((res => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "导出成功"
          })
        } else {
          this.$message.error(res.msg)
        }
      }))
    },
    showDetails (row) {
      this.$router.push({
        path: "/user/userDetails",
        query: {
          userId: row.backOrderId
        }
      })
    },
    handleSizeChange (v) {
      console.log(v)
      this.searchForm.size = v;
      this.getTableData(this.searchForm.current, v, this.searchForm.info, this.searchForm.sid);
    },
    handleCurrentChange (v) {
      this.searchForm.current = v;
      this.getTableData(v, this.searchForm.size, this.searchForm.info, this.searchForm.sid);
    },
    getTableData (curr, size, beginTime, endTime, keywords, info) {
      this.$api.finance.getUserOrderCostList({
        current: curr,
        size: size,
        keyword: keywords,
        endTime: endTime,
        beginTime: beginTime,
        orderNo: info,
      }).then(res => {
        if (res.code == 200) {
          console.log(res.data);
          let result = res.data.ipage.records;
          let currsku = res.data.skuList;
          result.forEach((item) => {
            currsku.forEach((op) => {
              if (item.backOrderId == op.backOrderId) {
                item.brandName = op.brandName;
                item.backSkuId = op.backSkuId;
                item.backCount = op.backCount;
                item.price = op.price;
                item.total = parseFloat(op.price) * parseFloat(op.backCount)
              }
            })
          })
          this.topData = res.data.allMoney;
          this.totalmoney = res.data.userTotalBackMoney
          this.tableData = result;
          console.log(this.tableData, '1231231')
          this.searchForm.totalCount = res.data.ipage.total;
          this.searchForm.size = res.data.ipage.size
        }
      })
    },
    chageYearData () {
      this.$api.user.getAdminUserCount({
        year: this.yearData
      }).then(res => {
        if (res.code == 200) {
          this.topData = res.data
        }
      })
    },
    getYearData () {
      this.$api.user.getAdminUserCount().then(res => {
        if (res.code == 200) {
          this.topData = res.data;
        }
      })
    },
    handleReset () {
      this.searchForm={
           name:'',
           keywords:'',
           date:[],
      }
      this.$refs.searchForm.resetFields();
      this.getTableData(this.searchForm.current, this.searchForm.size);
    },
    search () {
      if (this.searchForm.date != undefined && this.searchForm.date.length > 0) {
        this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.date[0], this.searchForm.date[1], this.searchForm.keywords, this.searchForm.info)
      } else {
        this.getTableData(this.searchForm.current, this.searchForm.size, "", "", this.searchForm.keywords, this.searchForm.info)
      }
    },
    btnlist () {
      //获取接口
      this.$api.city.getList({}).then(d => {
        if (d.code == 200) {
          this.tableData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    add () {

      this.btnForm = {
        "btnIcon": "",
        "btnName": "",
        "btnCode": "",
        "sort": 0
      }
      this.addORUpdateVisible = true;
    },
    update () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      this.btnForm = this.tableSelection[0];
      this.addORUpdateVisible = true;
    },
    del () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }

    },
    refresh () {
      this.btnlist();
    },
    jiesuan(){
      if (this.tableSelection .length != 1) {
        this.$message.error("请选中一行数据");
        return;
      }
      this.dialogVisible1 = true;
      this.jiesuanlist(this.tableSelection[0].years);

    },
    jiesuanlist(year){
      this.$api.outorder.jiesuanhao(
        {
          month:year
        }
 
      ).then(d => {
              if (d.code == 200) {
                 this.huishoulist = d.data;
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })


    },    
    enable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.state == 1) {
        this.$message.error('该数据已启用');
        return
      }
      this.$confirm('此操作将启用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.user.updateStatus({ userId: demo.wxUserId, status: 1 }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.sid);
            this.$message.success('启用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    disable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      console.log(demo)
      if (demo.state == 2) {
        this.$message.error('该数据已禁用');
        return
      }
      this.$confirm('此操作将禁用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.user.updateStatus({ userId: demo.wxUserId, status: 2 }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.sid);
            this.$message.success('禁用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
      export(){
       console.log("导出")
       this.$api.outorder
        .gethuishouzhichuOutOrderExcel({ 

        })
        .then((d) => {
          let filename = "会员累计回收支出明细表.xlsx";
          ExportExcel(d, filename); 
        })
        .catch((e) => {
          console.log(e);
        });

    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.tableData.findIndex(c => c.btnCode == this.btnForm.btnCode) >= 0) {
            this.$message.error('code不能重复');
            return
          }
          if (this.funTit == 'add') {
            this.$api.system.btnadd(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          } else {
            this.$api.system.btnupdate(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}

</script>
<style lang="less" scoped>
.exports-btn {
  margin-left: 20px;
}
.title-style {
  font-size: 25px;
}
.marbt20px {
  margin-bottom: 20px;
}
.fontText {
  font-size: 20px;
}
.fontTop {
  font-size: 40px;
}
.top-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
}
.top-item {
  border: 1px gray solid;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 20%;
  padding: 20px;
  .top-icon {
    width: 35%;
    height: 35%;

  }
  .right-item-box {
    text-align: center;
    margin-left: 20px;
  }
}
</style>